package cn.bandeqing.frame.mybatis.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.logicdelete.impl.BooleanLogicDeleteProcessor;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer {

    public MyBatisFlexConfiguration() {
        AuditManager.setAuditEnable(true);
        AuditManager.setMessageCollector(auditMessage -> {
            String fullSql = auditMessage.getFullSql();
            MDC.put("full_sql", fullSql);

            long queryTime = auditMessage.getQueryTime();
            MDC.put("query_time", String.valueOf(queryTime));

            long elapsedTime = auditMessage.getElapsedTime();
            MDC.put("elapsed_time", String.valueOf(elapsedTime));

            log.info(">>>>>> {}, {} ms", auditMessage.getFullSql(), auditMessage.getElapsedTime());
        });

        LogicDeleteManager.setProcessor(new BooleanLogicDeleteProcessor());

//        FlexGlobalConfig globalConfig = FlexGlobalConfig.getDefaultConfig();
//        globalConfig.setLogicDeleteColumn(null);
    }

    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        globalConfig.setLogicDeleteColumn(null);
        globalConfig.setVersionColumn(null);
    }

}
